""" This file find all audio filenames in DESED dataset.
Goal: Find DESED-related data from the entire AudioSet.
Usage: Avoid extracting files that appearred in DESED from the AudioSet. 
"""

import pandas as pd
from glob import glob
import os

all_tsv_files = glob("../../configs/dataset/desed_tsv/*.tsv")
# remove synth dataset
all_tsv_files = [f for f in all_tsv_files if "synth" not in f]
# collect all 'filename' columns from and save to a list
all_filenames = []
for tsv_file in all_tsv_files:
    df = pd.read_csv(tsv_file, sep="\t")
    all_filenames.extend(df["filename"].unique().tolist())
    print("Processing:", tsv_file, "with", len(all_filenames), "rows.")
# remove duplicates and wav extensions
all_filenames = [f.replace(".wav", "") for f in all_filenames]

if not os.path.exists("../src"):
    os.makedirs("../src")
# save to a json file
import json
with open("../src/DESED_all_filenames.json", "w") as f:
    json.dump(all_filenames, f, indent=4)


# remove unlabeled files from DESED
all_tsv_files = [f for f in all_tsv_files if "unlabeled" not in f]
# repeat the process
all_filenames = []
for tsv_file in all_tsv_files:
    df = pd.read_csv(tsv_file, sep="\t")
    all_filenames.extend(df["filename"].unique().tolist())
    print("Processing:", tsv_file, "with", len(all_filenames), "rows.")
# remove duplicates and wav extensions
all_filenames = [f.replace(".wav", "") for f in all_filenames]  
# save to a json file
with open("../src/DESED_supervised_filenames.json", "w") as f:
    json.dump(all_filenames, f, indent=4)
